//
//
//
#include "dlist.h"
#include <stdlib.h>

#include <stdio.h>

int find_max(dlist d_list);
int get_total(dlist d_list);

//
int main(void)
{
    dlist d_list;
    d_list = dl_create();

    //    dl_print(d_list);

    // test API of dlist
    dlist p = d_list;
    int i;
    int *value;
    for (i = 0; i < 10; i++) {
        value = (int *)malloc(sizeof(int));
	if (value == NULL) {
	    return 0;
	}
        *value = i;
	dl_add_after(p, (void *)value);
	p = dl_next(p);
    }

    int max = find_max(d_list);
    int total = get_total(d_list);
    //    printf("max is %d, total is %d\n", max, total);

    p = d_list;
    //    int j = 0;
    for (i = 0; i < 10; i++) {
        value = dl_remove_after(p);
	if (value != NULL) {
	    free(value);
	    //	    j++;
	}
    }

    //    printf("free %d value\n", j);
    //    dl_print(d_list);
    dl_destroy(d_list);
    return 1;
}

//
//
//
int
find_max(dlist dl)
{
    dlist p = dl_next(dl);
    int max = 0;
    int tmp = 0;

    max = *((int *)dl_get_value(p));
    while (p != NULL) {
        tmp = *((int *)dl_get_value(p));
	if (tmp > max) {
	    max = tmp;
	}
	p = dl_next(p);
    }
    return max;
}

//
//
//
int
get_total(dlist dl)
{
    dlist p = dl_next(dl);
    int total = 0;

    while (p != NULL) {
        total += *((int *)dl_get_value(p));
	p = dl_next(p);
    }
    return total;
}
